package control;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import log.Log;
import service.CardService;
import service.MemberService;
import entity.Card;
import entity.Member;

//main函数执行类
public class iyoga {
	
	//数据库的连接
	public static void main(String args[]) {

		//根据配置文件创建log
		Log log = Log.getInstance("src/propertiesFile/log.properties");
		log.logger = Logger.getLogger("Controller.class");
		//获取要执行的方法名
		String methodName = args[0].substring(0,3)+args[0].substring(4,5).toUpperCase() + args[0].substring(5);
		//根据反射创建Method对象
		Method method = null;
		//要加入的会员
		Member member ;
		//要办的卡
		Card card ;
		//添加Member的服务
		MemberService memberService = new MemberService();
		//添加card的服务
		CardService cardService = new CardService();
		
		try {
			//如果方法名是addMember则执行MemberService的addMember方法，如果
			//方法名是addCard则执行CardService的addCard方法
			if(methodName.equals("addMember")){
				member = new Member(args[1],args[2],args[3]);
				method = MemberService.class.getDeclaredMethod(methodName, Member.class);
				method.invoke(memberService, member);
			} else if(methodName.equals("addCard")){
				
				method = CardService.class.getDeclaredMethod(methodName, Card.class,String.class);
				int times;
				//如果命令行的参数不够4个说明是年卡，则设置times=0，否则设置成arg[3]
				if(args.length < 4){
					times = 0;
				} else {
					times = Integer.parseInt(args[3]);
				}
				card = new Card(args[2],times);
				method.invoke(cardService, card,args[1]);
			} else {
				System.out.println("输入有误,请按照add-xx value1 value2..的方式输入~~");
			}
		} catch (Exception e){
			e.printStackTrace();
			log.logger.debug("获得命令行参数出现问题！");
		}
		
	}
}
